<template>
  <div class="container">
    <div class="left">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>菜谱管理</h3>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入搜索内容" v-model="val"
              @keyup.enter.native="findAllMenu" clearable>
              <el-button slot="append" icon="el-icon-search"
                @click="findAllMenu"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-table :data="menuList" style="width: 100%" border>
          <!-- id -->
          <el-table-column prop="_id" label="菜谱编号">
          </el-table-column>
          <!-- 名称 -->
          <el-table-column prop="name" label="名称">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeData.name"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <!-- 缩略图 -->
          <el-table-column prop="coverpic" label="缩略图">
            <template width="95" slot-scope="scope">
              <img style="width: 95px; height: 95px; border: none"
                :src="scope.row.coverpic" v-show="!scope.row.isEdit" />
              <input type="file" @change="upImg" v-show="scope.row.isEdit" />
            </template>
          </el-table-column>
          <!-- 浏览量 -->
          <el-table-column prop="pageview" label="浏览量">
          </el-table-column>
          <!-- 收藏量 -->
          <el-table-column prop="collections" label="收藏量">
          </el-table-column>
          <!-- 是否免费 -->
          <el-table-column prop="" label="是否免费">
            <template width="90" slot-scope="scope">
              <el-switch v-model="changeData.isFree=='Y'?true:false"
                v-show="scope.row.isEdit"
                @change="isFreeChange(changeData.isFree)">
              </el-switch>
              <span
                v-show="!scope.row.isEdit">{{ scope.row.isFree=='Y'?'是': '否'}}</span>
            </template>
          </el-table-column>
          <!-- 是否热点 -->
          <el-table-column prop="" label="是否热点">
            <template width="90" slot-scope="scope">
              <el-switch v-model="changeData.isHot=='Y'?true:false"
                v-show="scope.row.isEdit"
                @change="isHotChange(changeData.isHot)">
              </el-switch>
              <span
                v-show="!scope.row.isEdit">{{ scope.row.isHot=='Y'?'是': '否'  }}</span>
            </template>
          </el-table-column>
          <!-- 原料 -->
          <el-table-column prop="ingredient" label="原料" width="100">
            <template slot-scope="scope">
              <el-popover placement="left" width="250" trigger="click">
                <el-table :data="scope.row.ingredient" height='400'>
                  <el-table-column width="150" property="name" label="菜品名称">
                  </el-table-column>
                  <el-table-column width="100" property="number" label="用量">
                  </el-table-column>
                </el-table>
                <!-- 查看 -->
                <el-button size="mini" slot="reference" type="success">查看
                </el-button>
              </el-popover>

            </template>
          </el-table-column>
          <!-- 制作步骤 -->
          <el-table-column prop="method" label="制作步骤" width="100">
            <template slot-scope="scope">
              <el-popover placement="left" width="400" trigger="click">
                <el-table :data="scope.row.method" height='600'>
                  <el-table-column width="200" property="describe" label="描述">
                  </el-table-column>
                  <el-table-column label="缩略图">
                    <template width="200" slot-scope="scope">
                      <img style="width: 100px; height: 100px; border: none"
                        :src="scope.row.img" />
                    </template>
                  </el-table-column>
                </el-table>
                <!-- 查看 -->
                <el-button size="mini" slot="reference" type="success">查看
                </el-button>
              </el-popover>

            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column prop="gender" label="操作">
            <template slot-scope="scope">
              <!-- 修改 -->
              <el-button size="mini" type="primary" v-show="!scope.row.isEdit"
                icon="el-icon-edit" @click="change(scope.row._id)">
                修改
              </el-button>
              <!-- 保存 -->
              <el-button size="mini" type="success" v-show="scope.row.isEdit"
                @click="upChange">
                <i class="el-icon-upload el-icon--right"></i>保存
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pages">
          <el-pagination @size-change="changeSize"
            @current-change="currentChange"
            :current-page="queryInfo.currentPage" :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState: imgState, mapActions: imgAction } = createNamespacedHelpers("upimg");
export default {
  data() {
    return {
      menuList: [],
      changeData: {},
      changeNum: 0,
      queryInfo: { pageSize: 5, currentPage: 1 },
      val: "",
      total: 0,
    };
  },
  computed: {
    ...imgState(['imgsrc'])
  },
  methods: {
    ...imgAction(["upImg"]),
    // 获取菜谱
    async findAllMenu() {
      // 发请求
      let { data } = await this.$api.menuApi.searchMenu({ ...this.queryInfo, val: this.val });
      let arr = data.menus
      arr.forEach((v) => {
        v.isEdit = false;
      });
      this.menuList = arr

      this.total = data.total;

      console.log(data);

      // classtips: (6) ['川菜', '木耳', '家常菜', '家常', '下酒菜', '胡萝卜']
      // collections: 24
      // coverpic: "https://cp1.douguo.com/upload/caiku/0/4/3/yuan_041fdcdeeef96417606f570c2d119413.jpg"
      // grade: "2"
      // ingredient: (16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
      // isEdit: false
      // isFree: "Y"
      // isHot: "Y"
      // isRecommend: "N"
      // method: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
      // name: "鱼香肉丝"
      // needtime: "30"
      // pageview: 704
      // state: "1"
    },
    //删除
    deleteSwiper(_id) {
      this.$api.menuApi.deleteSwiper({ _id }).then(({ data }) => {
        console.log(data);
        if (data.code == 405) {
          this.$message.error(data.error);
          return
        }
        // 删除提示
        this.$message.success(data.message);
        this.findAllMenu();

      });
    },
    // 修改
    change(id) {
      if (this.changeNum) return this.$message.error("请先完成当前修改");
      this.changeNum = 1;
      let res = this.menuList.filter((v) => v._id == id)[0];
      res.isEdit = true;
      this.changeData = { ...res };
      console.log(this.changeData);
    },
    // 确认修改
    upChange() {
      this.$api.menuApi
        .updateMenu({ ...this.changeData })
        .then(({ data }) => {
          console.log(data);
          this.$message.success(data.meta.msg);
          this.findAllMenu();
          this.changeNum = 0;
        });
    },
    // 每一页展示数量
    changeSize(i) {
      this.queryInfo.pageSize = i;
    },
    // 页数
    currentChange(i) {
      this.queryInfo.currentPage = i;
    },
    //是否免费修改
    isFreeChange(str) {
      console.log(str);
      if (str == 'Y') {
        this.changeData.isFree = 'N'
      }
      else {
        this.changeData.isFree = 'Y'
      }
    },
    isHotChange(str) {
      console.log(str);
      if (str == 'Y') {
        this.changeData.isHot = 'N'
      }
      else {
        this.changeData.isHot = 'Y'
      }
    }
  },
  created() {
    this.findAllMenu();
  },
  watch: {
    queryInfo: {
      handler() {
        this.findAllMenu();
      },
      deep: true
    }
  }
};
</script>

<style lang="scss" scoped>
img {
  width: 50px;
  height: 50px;
}

input {
  width: 100px;
}
.container {
  display: flex;
  .left {
    width: 100%;
  }
}
</style>
